#include "baseframemove.h"

#include <QDebug>
#include <QGuiApplication>
#include <QScreen>

//事件过滤器方法
baseFrameMove::baseFrameMove(QWidget *parent) : QWidget(parent)
{
    m_isPressed = false;
    m_moveable = this->geometry();
    installEventFilter(this);  // 安装事件过滤器
}

bool baseFrameMove::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == this){
        switch (event->type()) {
        case QEvent::MouseButtonPress:{
            //最安全的转换方式
            QMouseEvent *mouseevent = dynamic_cast<QMouseEvent*>(event);
            if(mouseevent->button() == Qt::LeftButton){
                m_point = mouseevent->pos();
                m_isPressed = true;
                return true; // 拦截事件
            }
            break;
        }
        case QEvent::MouseMove:{
            if(m_isPressed) {
                QMouseEvent *mouseevent = dynamic_cast<QMouseEvent*>(event);
                QPoint delta = mouseevent->pos() - m_point;
                QPoint newPos = this->pos() + delta;

//                // 获取主屏幕的几何信息
//                QScreen *screen =QGuiApplication::primaryScreen();
//                QRect screenRect =screen->availableGeometry();

//                //限制窗口的移动范围
//                newPos.setX(qBound(screenRect.left(),newPos.x(),screenRect.right()- width()));
//                newPos.setY(qBound(screenRect.top(),newPos.y(),screenRect.bottom()- height()));

                this->move(newPos);

                return true; // 拦截事件
            }
            break;
        }
        case QEvent::MouseButtonRelease:{
            QMouseEvent *mouseevent = dynamic_cast<QMouseEvent*>(event);
            if (mouseevent->button() == Qt::LeftButton) {
                m_isPressed = false;
                return true; // 拦截事件
            }
            break;
        }
        default:
            break;
        }
    }
    return QWidget::eventFilter(watched, event);
}


/*
//重写鼠标事件
baseFrameMove::baseFrameMove(QWidget *parent) : QWidget(parent)
{
    m_isPressed = false;
    m_moveable = this->geometry();
}

void baseFrameMove::mousePressEvent(QMouseEvent *event)
{
    //判断是左键
    if(event->button() == Qt::LeftButton){
        m_point = event->pos();
        // 判断m_Point是否在点击的窗口范围内,如果是,则 m_isPressed = true,否则返回
        //m_isPressed = m_moveable.contains(m_point);
        m_isPressed = true;
    }
}

void baseFrameMove::mouseReleaseEvent(QMouseEvent *)
{
    m_isPressed = false;
}

//void Widget::mouseDoubleClickEvent(QMouseEvent *event)
//{

//}

void baseFrameMove::mouseMoveEvent(QMouseEvent *event)
{
    if(!m_isPressed){
        return;
    }
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
    this->move(this->pos() + event->pos() - m_point);
#else
    QPoint delta = event->pos() - m_point;
    QPoint newPos = this->pos() + delta;

    // 获取主屏幕的几何信息
    QScreen *screen =QGuiApplication::primaryScreen();
    QRect screenRect =screen->availableGeometry();
    //限制窗口的移动范围
    newPos.setX(qBound(screenRect.left(),newPos.x(),screenRect.right()- width()));
    newPos.setY(qBound(screenRect.top(),newPos.y(),screenRect.bottom()- height()));

    this->move(newPos);
#endif
}
*/

